package controllers

import (
	"net/http"
	"time"

	"gitee.com/diandisoft/ant_forest_energy_web_server/db"
	"gitee.com/diandisoft/ant_forest_energy_web_server/models"
	"github.com/gin-gonic/gin"
)

type CollentController struct{}

// 新增采集记录
// /:user/collect
func (u CollentController) CollentRecords(c *gin.Context) {
	if user := c.Param("user"); user != "" {
		var r = models.CollentRecords{}
		if err := c.ShouldBindJSON(&r); err == nil {
			dao := new(db.Dao)
			if err = dao.InsertCollentRecords(r, user); err != nil {
				c.Error(err)
			} else {
				c.JSON(http.StatusOK, gin.H{"result": r})
			}
		} else {
			c.Error(err)
		}
	}

}

// 返回某个账号的最后采集数据的时间
// 客户端拿这个时间与支付宝的数据时间进行对比，只提交新数据给CollentRecords接口
func (u CollentController) LastCollentTime(c *gin.Context) {
	if user := c.Param("user"); user != "" {
		var r time.Time
		dao := new(db.Dao)
		if err := dao.GetLastCollentTime(user, &r); err != nil {
			c.Error(err)
		} else {
			c.JSON(http.StatusOK, gin.H{"result": r.Unix()})
		}
	}
}

// 新增被采集记录
func (u CollentController) WithCollentRecords(c *gin.Context) {
	return
}
